﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class CRM_GYS_N : FormBase1
    {
        public delegate void DoAction();//如果有参数的话，定义参数 

        public DoAction FatherRefresh;
        private int count = 0;
        public CRM_GYS_N()
        {
            InitializeComponent();
        }

        private void btn_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SVC.client.CRM_Client cclient = new SVC.client.CRM_Client();
            cclient.Adress = te_address.Text;
            if (lue_AreaCode.EditValue != null) cclient.AreaCode = lue_AreaCode.EditValue.ToString();
            cclient.balance = 0;
            cclient.bankname = te_bankname.Text;
            cclient.bankNo = te_bankno.Text;
            cclient.ClientID = Guid.NewGuid();
            cclient.ClientName = te_ClientName.Text;
            cclient.Code = te_code.Text;
            if (lue_CooperationID.EditValue != null) cclient.CooperationID = int.Parse(lue_CooperationID.EditValue.ToString());
            cclient.credit = 0;
            cclient.custom1 = te_custom1.Text;
            cclient.custom2 = te_custom2.Text;
            if (lue_empid.EditValue!=null)  cclient.EmplID=new Guid(lue_empid.EditValue.ToString());
            if (lue_IndustryCode.EditValue != null) cclient.IndustryCode = int.Parse(lue_IndustryCode.EditValue.ToString());
            cclient.IsDouble = true;
            cclient.Remarks = te_Remarks.Text;
            cclient.WebSite = te_tel.Text;
            cclient.ShortName = te_shortname.Text;
            if (lue_SourceID.EditValue!=null)  cclient.SourceID = int.Parse(lue_SourceID.EditValue.ToString());
            cclient.suramt = 0;
            cclient.taxid = te_taxid.Text;
            if (lue_worth.EditValue != null) cclient.WorthID = int.Parse(lue_worth.EditValue.ToString());
            string r = my.bw.CRM_Client_Add(cclient, my.ACID);
            if (string.IsNullOrEmpty(r))
            {
                count++;
                resetUI();
                
            }
            else
            {
                Comm.ShowErorr(r);
            }
        }

        private void resetUI()
        {

            te_code.Text = "";
            te_shortname.Text = "";
            te_ClientName.Text = "";
            lue_worth.EditValue = null;
            lue_CooperationID.EditValue = null;
            lue_AreaCode.EditValue = null;
            lue_IndustryCode.EditValue = null;
            lue_empid.EditValue = null;
            lue_SourceID.EditValue = null;
            te_tel.Text = "";
            te_bankname.Text = "";
            te_bankno.Text = "";
            te_taxid.Text = "";
            te_address.Text = "";
            te_Remarks.Text = "";
            te_custom1.Text = te_custom2.Text = "";
        }

        private void CRM_Client_N_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (count > 0)
            {
                FatherRefresh();
            }
        }

        private void btn_newclose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SVC.client.CRM_Client cclient = new SVC.client.CRM_Client();
            cclient.Adress = te_address.Text;
            if (lue_AreaCode.EditValue != null) cclient.AreaCode = lue_AreaCode.EditValue.ToString();
            cclient.balance = 0;
            cclient.bankname = te_bankname.Text;
            cclient.bankNo = te_bankno.Text;
            cclient.ClientID = Guid.NewGuid();
            cclient.ClientName = te_ClientName.Text;
            cclient.Code = te_code.Text;
            if (lue_CooperationID.EditValue != null) cclient.CooperationID = int.Parse(lue_CooperationID.EditValue.ToString());
            cclient.credit = 0;
            cclient.custom1 = te_custom1.Text;
            cclient.custom2 = te_custom2.Text;
            if (lue_empid.EditValue != null) cclient.EmplID = new Guid(lue_empid.EditValue.ToString());
            if (lue_IndustryCode.EditValue != null) cclient.IndustryCode = int.Parse(lue_IndustryCode.EditValue.ToString());
            cclient.IsDouble = true;
            cclient.Remarks = te_Remarks.Text;
            cclient.WebSite = te_tel.Text;
            cclient.ShortName = te_shortname.Text;
            if (lue_SourceID.EditValue != null) cclient.SourceID = int.Parse(lue_SourceID.EditValue.ToString());
            cclient.suramt = 0;
            cclient.taxid = te_taxid.Text;
            if (lue_worth.EditValue != null) cclient.WorthID = int.Parse(lue_worth.EditValue.ToString());
            string r = my.bw.CRM_Client_Add(cclient, my.ACID);
            if (string.IsNullOrEmpty(r))
            {
                count++;
                this.Close();

            }
            else
            {
                Comm.ShowErorr(r);
            }
        }

        private void CRM_GYS_N_Load(object sender, EventArgs e)
        {
            lue_AreaCode.Properties.DataSource = my.bw.Area_Get("AreaCode", my.ACID);
            lue_CooperationID.Properties.DataSource = my.bw.CRM_Cooperatie_Get("CooperationID", my.ACID);
            lue_empid.Properties.DataSource = my.bw.View_Emp_lite_List("", my.ACID);
            lue_IndustryCode.Properties.DataSource = my.bw.CRM_Industry_Get("IndustryCode", my.ACID);
            lue_SourceID.Properties.DataSource = my.bw.CRM_ClientSource_Get("SourceID", my.ACID);
            lue_worth.Properties.DataSource = my.bw.CRM_Worth_Get("WorthID", my.ACID);
        }

        private void lue_worth_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                CRM_Worth_N frm = new CRM_Worth_N();
                frm.FatherRefresh = bindWorth;
                frm.ShowDialog();
            }
        }
        void bindWorth(string WorthTitle)
        {
            lue_worth.Properties.DataSource = my.bw.CRM_Worth_Get("WorthID", my.ACID);
            lue_worth.Text = WorthTitle;
        }

        private void lue_CooperationID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                CRM_Cooperatie_N frm = new CRM_Cooperatie_N();
                frm.FatherRefresh = bindCooperatie;
                frm.ShowDialog();
            }
        }
        void bindCooperatie(string Cooperatie)
        {
            lue_CooperationID.Properties.DataSource = my.bw.CRM_Cooperatie_Get("CooperationID", my.ACID);
            lue_CooperationID.Text = Cooperatie; 
        }

        private void lue_SourceID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                CRM_ClientSource_N frm = new CRM_ClientSource_N();
                frm.FatherRefresh = bindClientSource;
                frm.ShowDialog();
            }
        }
        void bindClientSource(string sourceID)
        {
            lue_SourceID.Properties.DataSource = my.bw.CRM_ClientSource_Get("SourceID", my.ACID);
            lue_SourceID.EditValue = sourceID;
        }

        private void lue_IndustryCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                CRM_Industry_N frm = new CRM_Industry_N();
                frm.FatherRefresh = bindIndustryCode;
                frm.ShowDialog();
            }
        }

        void bindIndustryCode(string IndustryCode)
        {
            lue_IndustryCode.Properties.DataSource = my.bw.CRM_Industry_Get("IndustryCode", my.ACID);
            lue_IndustryCode.Text=IndustryCode;
        }
    }
    
}